package sfeir.ergosum.menu;

import sfeir.ergosum.AboutActivity;
import sfeir.ergosum.AddContactActivity;
import sfeir.ergosum.ContactManagerActivity;
import sfeir.ergosum.HiveActivity;
import sfeir.ergosum.MyProfileActivity;
import sfeir.ergosum.PreferencesActivity;
import sfeir.ergosum.R;
import sfeir.ergosum.myprofile.SelectProfileActivity;
import sfeir.ergosum.service.ContactService;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MenuService {
    public static final int REFRESH = 100;
    public final static int MENU_MY = 0x01;
    public final static int MENU_ADD = 0x02;
    public final static int MENU_GROUP = 0x03;
    public final static int MENU_UPDATE = 0x04;
    public final static int MENU_PREF = 0x05;
    public final static int MENU_ABOUT = 0x06;

    public static boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_MY, 0, R.string.main_menu_my).setIcon(R.drawable.identity);
        menu.add(0, MENU_ADD, 0, R.string.main_menu_add).setIcon(R.drawable.add_user);
        menu.add(0, MENU_GROUP, 0, R.string.add_group).setIcon(R.drawable.add_group);
        menu.add(0, MENU_UPDATE, 0, R.string.main_menu_update).setIcon(R.drawable.refresh);
        menu.add(0, MENU_PREF, 0, R.string.main_menu_preference).setIcon(R.drawable.utilities);
        menu.add(0, MENU_ABOUT, 0, R.string.main_menu_about).setIcon(R.drawable.about);
        return true;
    }

    public static boolean onMenuItemSelected(Activity activity, MenuItem item) {
        switch (item.getItemId()) {
        case MENU_MY:
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
            String key = prefs.getString("profile_spreadsheet_key", null);
            if (null == key) {
                activity.startActivity(new Intent(activity, SelectProfileActivity.class));
            } else {
                startProfileDetailsActivity(activity, key);
            }
            return true;
        case MENU_GROUP:
            addGroup(activity);
            return true;
        case MENU_UPDATE:
            ContactService.actionUpdateContact(activity);
            Toast.makeText(activity, R.string.update_start, 500).show();
            return true;
        case MENU_ADD:
            activity.startActivityForResult(new Intent(activity, AddContactActivity.class), REFRESH);
            return true;
        case MENU_PREF:
            activity.startActivity(new Intent(activity, PreferencesActivity.class));
            return true;
        case MENU_ABOUT:
            activity.startActivity(new Intent(activity, AboutActivity.class));
            return true;
        default:
            break;
        }
        return false;
    }

    public static void startProfileDetailsActivity(Activity activity, String key) {
        Intent intent = new Intent(activity, MyProfileActivity.class);
        intent.putExtra("key", key);
        activity.startActivityForResult(intent, MenuService.REFRESH);
    }

    private static void addGroup(final Activity activity) {
        LayoutInflater factory = LayoutInflater.from(activity);
        final View textEntryView = 
            factory.inflate(R.layout.input_text_dialog, null);
        final TextView textInstruction = 
            (TextView) textEntryView.findViewById(R.id.instruction_dialog);
        final EditText text = 
            (EditText) textEntryView.findViewById(R.id.input_dialog);

        textInstruction.setText(R.string.add_group_instruction);

        AlertDialog dialog =  new AlertDialog.Builder(activity)
        .setTitle(R.string.add_group)
        .setView(textEntryView)
        .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                /* User clicked OK so do some stuff */
                String nameGroup;
                if (text.getText().length() == 0)
                    nameGroup = activity.getString(R.string.add_default_name_group);
                else
                    nameGroup = text.getText().toString().trim();

                ContactManagerActivity.addGroup(activity, nameGroup);
                HiveActivity.startActivity(activity, "", "");
            }
        }).setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                /* User clicked cancel so do some stuff */
            }
        }).create();
        dialog.show();
    }
}
